package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.service.ITInventoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TStocktaking;
import com.ruoyi.system.service.ITStocktakingService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 盘点 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-08
 */
@Controller
@RequestMapping("/system/tStocktaking")
public class TStocktakingController extends BaseController
{
    private String prefix = "system/tStocktaking";
	
	@Autowired
	private ITStocktakingService tStocktakingService;
	@Autowired
	private ITInventoryService itInventoryService;

	
	@RequiresPermissions("system:tStocktaking:view")
	@GetMapping()
	public String tStocktaking(ModelMap mmap)
	{
		if(!String.valueOf(ShiroUtils.getSysUser().getDeptId()).equals("100")){
			mmap.put("deptId",ShiroUtils.getSysUser().getDeptId());
		}
	    return prefix + "/tStocktaking";
	}
	
	/**
	 * 查询盘点列表
	 */
	@RequiresPermissions("system:tStocktaking:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(TStocktaking tStocktaking)
	{
		startPage();
        List<TStocktaking> list = tStocktakingService.selectTStocktakingList(tStocktaking);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出盘点列表
	 */
//	@RequiresPermissions("system:tStocktaking:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TStocktaking tStocktaking)
    {
    	List<TStocktaking> list = tStocktakingService.selectTStocktakingList(tStocktaking);
        ExcelUtil<TStocktaking> util = new ExcelUtil<TStocktaking>(TStocktaking.class);
        return util.exportExcel(list, "盘点数据");
    }
	
	/**
	 * 新增盘点
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存盘点
	 */
	@RequiresPermissions("system:tStocktaking:add")
	@Log(title = "盘点", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(TStocktaking tStocktaking)
	{		
		return toAjax(tStocktakingService.insertTStocktaking(tStocktaking));
	}

	/**
	 * 修改盘点
	 */
	@GetMapping("/edit/{stockId}")
	public String edit(@PathVariable("stockId") Long stockId, ModelMap mmap)
	{
		TStocktaking tStocktaking = tStocktakingService.selectTStocktakingById(stockId);
		mmap.put("tStocktaking", tStocktaking);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存盘点
	 */
	@RequiresPermissions("system:tStocktaking:edit")
	@Log(title = "盘点", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(TStocktaking tStocktaking)
	{		
		return toAjax(tStocktakingService.updateTStocktaking(tStocktaking));
	}

	
	/**
	 * 删除盘点
	 */
	@RequiresPermissions("system:tStocktaking:remove")
	@Log(title = "盘点", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(tStocktakingService.deleteTStocktakingByIds(ids));
	}

	/**
	 * 盘点设置
	 */
	@GetMapping("/setStocktaking")
	public String setStocktaking()
	{
		return prefix + "/setStocktaking";
	}

	/**
	 * 保存盘点设置
	 */
	@Log(title = "盘点", businessType = BusinessType.UPDATE)
	@PostMapping("/saveSetStocktaking")
	@ResponseBody
	public AjaxResult saveSetStocktaking(TStocktaking tStocktaking)
	{
		Long deptId = tStocktaking.getDeptId();
		//将所选门店下的商品设置为未盘商品
		int num = itInventoryService.updateTInventoryIsstocktaking(deptId,"0");

		//将该门店的已盘点的数据清空
		num += tStocktakingService.deleteByDeptId(deptId);

		return toAjax(0);
	}
}
